package com.li.controller.admin;

import com.li.DTO.AreaManageDTO;
import com.li.VO.PageListVO;
import com.li.pojo.AreaManage;
import com.li.pojo.RoomNumber;
import com.li.result.Result;
import com.li.service.SubregionManageService;
import com.li.util.ExcelPoi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;

/**
 * @Author: zhiqingLi
 * @Description: 小区管理
 * @Date: Created in 11:40 2024/3/13
 **/
@RestController
@RequestMapping("/admin/sub")
@Slf4j
public class SubregionManageController {

    @Autowired
    private SubregionManageService subregionManageService;

    //查询全部 加上模糊查询
    @PostMapping("/selShow")
    public Result<PageListVO> showList(@RequestBody AreaManageDTO areaManageDTO){
        log.info("信息传递：{}",areaManageDTO);
        PageListVO pageListVO =  subregionManageService.showList(areaManageDTO);
        return Result.success(pageListVO);
    }

    //添加
    @PostMapping("/add")
    public Result addSubregion(@RequestBody AreaManage areaManage){
        log.info("信息传递：{}",areaManage);
        subregionManageService.addSubregion(areaManage);
        return Result.success();
    }

    //修改回显
    @GetMapping("/upd/{id}")
    public Result<AreaManage> updGetById(@PathVariable("id") Long id){
        log.info("信息传递：{}",id);
        AreaManage areaManage = subregionManageService.updGetById(id);
        return Result.success(areaManage);
    }

    //修改数据
    @PostMapping("/updSubregion")
    public Result updSubregion(@RequestBody AreaManage areaManage){
        log.info("信息传递：{}",areaManage);
        subregionManageService.updSubregion(areaManage);
        return Result.success();
    }

    //删除
    @GetMapping("/del/{id}")
    public Result delById(@PathVariable("id") Long id){
        log.info("信息传递：{}",id);
        subregionManageService.delById(id);
        return Result.success();
    }

    //上传 excel 表格数据 // id 必然1
    @PostMapping("/fileUpload")
    public Result<String> fileUpload(@RequestPart("file") MultipartFile file, @RequestParam("id")Integer id) throws Exception {
        InputStream inputStream = file.getInputStream();
        List<AreaManage> read = ExcelPoi.read(inputStream, id);
        if (read.size()>0){
            for (AreaManage areaManage : read) {
                subregionManageService.addSubregion(areaManage);
            }
        }
        return Result.success();
    }
}
